/*
 * Copyright (c) 2004-2022, University of Oslo
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the name of the HISP project nor the names of its contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.hisp.dhis.analytics.event;

import java.util.Date;

import org.hisp.dhis.program.AnalyticsType;
import org.hisp.dhis.program.ProgramIndicator;
import org.hisp.dhis.relationship.RelationshipType;

/**
 * Component responsible for generating a complete sub-query which fetches the
 * aggregated values of the specified Program Indicator. This component is
 * designed to be invoked when listing events or enrollments and the list
 * requires an additional value derived from the Program Indicator
 * expression/filter.
 *
 * For instance:
 *
 * <pre>
 * SELECT
 *    pi,
 *    tei,
 *    ou, ...
 *    (select avg( PROGRAM INDICATOR SUBQUERY )) // generated by this component
 * FROM
 *    analytics_enrollment_uy2gu8kt1jf as ax // this is the "outer" sql entity
 * WHERE
 *    ...
 * </pre>
 *
 * An example of a generated sub-query is:
 *
 * <pre>
 * avg((date_part('year', age(cast(executiondate as date), cast("iESIqZ0R0R0" as date)))))
 * FROM analytics_event_uy2gu8kt1jf as subax"
 * </pre>
 *
 * Note that this component does not add the {@code SELECT } keyword to the
 * generated query.
 *
 * @author Luciano Fiandesio
 */
public interface ProgramIndicatorSubqueryBuilder
{
    /**
     * Generates the Program Indicator sub-query to be used as aggregation
     * column within a Enrollment/Event list query.
     *
     * @param programIndicator a {@see ProgramIndicator} object
     * @param outerSqlEntity a {@see AnalyticsType} object, representing the
     *        outer sql context
     * @param earliestStartDate reporting start date
     * @param latestDate reporting end date
     *
     * @return a String containing a Program Indicator sub-query
     */
    String getAggregateClauseForProgramIndicator( ProgramIndicator programIndicator, AnalyticsType outerSqlEntity,
        Date earliestStartDate, Date latestDate );

    /**
     * Generates the Program Indicator sub-query to be used as aggregation
     * column within a Enrollment/Event list query. This method accepts a
     * {@see RelationshipType} object, that is used to filter the Program
     * Indicator values by the type of relationship specified. For instance,
     * given a "Mother->Child" relationship type, this method will generated a
     * SQL that will fetch only the "Child" side of the relationship when
     * aggregating the value for the Program Indicator
     *
     * @param programIndicator a {@see ProgramIndicator} object
     * @param relationshipType a {@see RelationshipType} object
     * @param outerSqlEntity a {@see AnalyticsType} object, representing the
     *        outer sql context
     * @param earliestStartDate reporting start date
     * @param latestDate reporting end date
     *
     * @return a String containing a Program Indicator sub-query
     */
    String getAggregateClauseForProgramIndicator( ProgramIndicator programIndicator, RelationshipType relationshipType,
        AnalyticsType outerSqlEntity, Date earliestStartDate, Date latestDate );
}
